//
//  GroupRootViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/4.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "GroupRootViewController.h"
#import "YDSelectPopupView.h"
#import "I_Admin.h"
#import "ContactDataHelper.h"
#import "GroupForAdminCell.h"
#import "GroupForCoachCell.h"
#import "CoachDetailController.h"
#import "EditGymAdminController.h"
#import "AddGymAdminController.h"
#import "AddCoachViewController.h"

@interface GroupRootViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *adminList;
//@property (nonatomic, strong) NSMutableArray *coachList;
@property (nonatomic, strong) NSMutableArray *coachDataSource;
@property (nonatomic, strong) NSMutableArray *sectionTitles;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation GroupRootViewController

#pragma mark Class Method
+ (instancetype)controller
{
    GroupRootViewController *vc = [[GroupRootViewController alloc] init];
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"团队";
    [self setRightBarButtonWithTitle:@"添加成员" target:self action:@selector(addGroup)];
    [self configUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestGymAdminListAndCoachList];
}

#pragma mark UI
- (void)configUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    //修改右边索引的背景色
    tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    //修改右边索引字体的颜色
    tableView.sectionIndexColor = HEXColor(@"#4A4A4A");
    //修改右边索引点击时候的背景色
    //    tableView.sectionIndexTrackingBackgroundColor = HEXColor(@"#3AA7FF");
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GroupForAdminCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GroupForAdminCell class])];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GroupForCoachCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GroupForCoachCell class])];

    tableView.backgroundColor = HEXColor(@"F4F6F9");
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [UIView new];
    self.tableView = tableView;
}


#pragma mark NET
- (void)requestGymAdminListAndCoachList
{
    if (_adminList.count == 0 && _coachDataSource.count == 0) {
        [self.view showLoading];
    }
    WeakSelf(self);
    [I_Admin requestGymAdminListAndGymCoachListOnSuccess:^(NSMutableArray *adminList, NSMutableArray *coachList) {
        if (adminList.count == 0 && coachList.count == 0) {
            [weakself.view  showEmptyingGroupViewCallBackBlock:^{
                [weakself addGroup];
            }];
        } else {
            [weakself.view hideStatusView];
            weakself.adminList = adminList;
            [weakself.tableView reloadData];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                weakself.coachDataSource = [ContactDataHelper getCoachListDataBy:coachList];
                weakself.sectionTitles = [ContactDataHelper getCoachListSectionBy:weakself.coachDataSource];
                if (weakself.adminList.count > 0) {
                    [weakself.sectionTitles insertObject:UITableViewIndexSearch atIndex:0];
                } else {
                    [weakself.sectionTitles insertObject:@"" atIndex:0];
                }
                dispatch_sync(dispatch_get_main_queue(), ^{
                    [weakself.tableView reloadData];
                });
            });
        }
    } failure:^(NSError *error) {
        if (weakself.adminList.count == 0 && weakself.coachDataSource.count == 0) {
            [weakself.view showError:error refreshBlock:^{
                [weakself requestGymAdminListAndCoachList];
            }];
        }
    }];
}

#pragma mark Action
- (void)addGroup
{
    [YDSelectPopupView showWithTitles:@[@"添加店长",@"添加教练"] selectBlock:^(NSInteger selectIndex) {
        if (selectIndex == 0) {
            AddGymAdminController *addAdminVC = [AddGymAdminController controller];
            [self.navigationController pushViewController:addAdminVC animated:YES];
        } else {
            AddCoachViewController *addCoachVC = [AddCoachViewController controller];
            [self.navigationController pushViewController:addCoachVC animated:YES];
        }
    } cancleBlock:nil];
}

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return _coachDataSource.count + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _adminList.count;
    }
    return [_coachDataSource[section - 1] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        GroupForAdminCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GroupForAdminCell class])];
        M_Admin *admin = _adminList[indexPath.row];
        cell.admin = admin;
        return cell;
    } else {
        GroupForCoachCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GroupForCoachCell class])];
        M_Coach *coach = _coachDataSource[indexPath.section - 1][indexPath.row];
        cell.coach = coach;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 25;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }
    UILabel *label = [[UILabel alloc] init];
    [label setFont:MediumFont(14)];
    [label setTextColor:HEXColor(@"4A4A4A")];
    [label setBackgroundColor:HEXColor(@"F4F6F9")];
    [label setText:[NSString stringWithFormat:@"  %@",self.sectionTitles[section]]];
    return label;
    
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.sectionTitles;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        M_Admin *admin = _adminList[indexPath.row];
        EditGymAdminController *editAdminVC = [EditGymAdminController controllerWithAdmin:admin];
        [self.navigationController pushViewController:editAdminVC animated:YES];
    } else {
        M_Coach *coach = _coachDataSource[indexPath.section - 1][indexPath.row];
        CoachDetailController *coachDetailVC = [CoachDetailController controllerWithCoach:coach];
        [self.navigationController pushViewController:coachDetailVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

@end
